var spiralOrder = function(n) {
    if (n === 0) {
        return []
    }
    let rotateNum = Math.floor(n / 2)
    let forNum = n % 2 === 0 ? rotateNum : rotateNum + 1;
    let lenCol = n
    let lenRow = n
    let rsl = []
    let count = 0
    for (let i = 0; i < n; i++) {
        rsl[i] = []
        for (let j = 0; j < n; j++) {
            rsl[i].push([])
        }
    }
    for (let i = 0; i < forNum; i++) {
        for (let j = i; j < lenCol - i; j++) {
            rsl[i][j] = ++count
        }
        for (let k = i + 1; k < lenRow - i - 1; k++) {
            rsl[k][lenCol - i - 1] = ++count
        }
        if (lenRow - i - 1 > i) {
            for (let t = lenCol - i - 1; t >= i; t--) {
                rsl[lenRow - i - 1][t] = ++count
            }
        }
        if (lenCol - i - 1 > i) {
            for (let k = lenRow - i - 1 - 1; k > i; k--) {
                rsl[k][i] = ++count
            }
        }
    }
    return rsl
};
console.log(spiralOrder(3))